package com.hnevc.io;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class CharCodeDemo {

	public static void main(String[] args) throws IOException {
		// 读取utf-8格式字符文件
		{
			FileReader fr = new FileReader("D:\\datautf8.txt");
			BufferedReader reader = new BufferedReader(fr);
			String str = reader.readLine();
			System.out.println(str);
		}
		{
			// 读取gbk编码格式的字符文件
			FileInputStream fis = new FileInputStream("D:\\datagbk.txt");
			InputStreamReader reader = new InputStreamReader(fis, "gb2312");
			
			BufferedReader br = new BufferedReader(reader);
			String str = br.readLine();
			System.out.println(str);

		}
		// 写出字符到utf-8格式的字符文件
		{
			String temp = "你好Hello";
			FileWriter fw = new FileWriter("D:\\datautf81.txt");
			fw.write(temp);
			fw.close();
		}

		// 写出到gbk编码格式的字符文件
		{
			String temp = "你好Hello";
			OutputStream out = new FileOutputStream("D:\\datagbk1.txt");
			OutputStreamWriter writer = new OutputStreamWriter(out, "gbk");
			writer.write(temp);
			writer.close();
		}
		{
			byte[] utf8 = "中".getBytes("utf-8");
			byte[] gbk = "中".getBytes("gbk");
			byte[] test = "中".getBytes();
			System.out.println("df");
			
		}
	}

}
